package com.animal.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.animal.bean.ManagerBean;
import com.animal.dao.ManagerManage;;

@WebServlet(name="UpdateServlet",urlPatterns={"/UpdateServlet"})
public class UpdateServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public UpdateServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	        
		doPost(request,response);
	
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		String dos = request.getParameter("do");
		if (dos == null || dos.equals("")) {	
			response.sendRedirect("ExperimentalFileManager.html");	
		}
		
		//获取ExperimentalPersonalInfo.jsp页面中的input
		String ID=request.getParameter("ID");

		ManagerBean manager = ManagerManage.selectByID(ID);

		String Password=request.getParameter("Password");
		String Name=request.getParameter("Name");
		String Gender=request.getParameter("Gender");
		String PhoneNumber=request.getParameter("PhoneNumber");
		//String Position=request.getParameter("Position");
		String Email=request.getParameter("Email");
		
		//根据条件赋值
		if(Password==""||Password==null){
			Password=manager.getPassword();
		}
		if(Name==""||Name==null){
			Name=manager.getName();
		}
		if(Gender==""||Gender==null){
			Gender=manager.getGender();
		}
		if(PhoneNumber==""||PhoneNumber==null){
			PhoneNumber=manager.getPhoneNumber();
		}
//		if(Position==""||Position==null){
//			Position=manager.getPosition();
//		}
		if(Email==""||Email==null){
			Email=manager.getEmail();
		}
		boolean b=ManagerManage.update(ID,Password,Name,Gender,PhoneNumber,Email);
		if(b==true){
			out.print("<script>alert('修改成功!')</script>");
			if (dos.equals("changeperson")) {
				out.print("<script>window.location.href='ExperimentalPersonalInfo.jsp'</script>");
			}
			else if(dos.equals("changepassword")){
				out.print("<script>window.location.href='ExperimentalChangePassword.jsp'</script>");		
			}
			else if (dos.equals("changepaperinfo")) {
				out.print("<script>window.location.href='PaperPersonalInfo.jsp'</script>");
			}
			//request.getRequestDispatcher("ExperimentalPersonalInfo.jsp").forward(request,response); //转发
			//response.sendRedirect("ExperimentalFileManager.html");  //重定向
		}	
		else{
			out.print("<script>alert('修改失败!')</script>");
			out.print("Sorry! Your update is not successful!");
		}
		
        out.close();
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
